package models;

import java.util.List;

import javax.persistence.*;

import com.avaje.ebean.Page;
import com.avaje.ebean.validation.NotNull;

import play.db.ebean.*;
import play.db.jpa.JPA;

@Entity
public class Compartment extends Model {

	@Id
	public Long id;

	@NotNull
	public String compartmentKey;

	@ManyToOne
	@NotNull
	public Forest forest;

	@OneToOne
	public ForestRight forestRight;
	
//	public Float canopyDensity;

	//Finder
	public static Finder<Long, Compartment> find = new Finder<Long, Compartment>(
			Long.class, Compartment.class);
	
	public static Compartment getCompartment(Compartment compartment) {
		return find.fetch("forest").fetch("forestRight").where().idEq(compartment.id).findUnique();
	}
	
    /**
     * Return a page of Compartment
     *
     * @param page Page to display
     * @param pageSize Number of computers per page
     * @param sortBy Computer property used for sorting
     * @param order Sort order (either or asc or desc)
     * @param filter Filter applied on the name column
     */
    public static Page<Compartment> page(int page, int pageSize, String sortBy, String order, String filter) {
        return 
            find.where()
                .ilike("name", "%" + filter + "%")
                .orderBy(sortBy + " " + order)
                .fetch("forest").fetch("forestRight")
                .findPagingList(pageSize)
                .getPage(page);
    }
    
    /* 算法 */
    /*平均年龄
     *单位面积蓄积量
     *总蓄积量算法
     *郁闭度
     *面积
     *龄级
     */
    
    /*未完成
     *地位级
     */
    
    public Integer area(){
    	//计算面积
    	List <Subcompartment> subcompartment = Subcompartment.find.where().eq("subcompartment", this).findList();
    	int area = 0;
    	for( int i = 0; i < subcompartment.size(); i++){
    		area += subcompartment.get(i).area;
    	}
    	return area;
    }
    
    public double averageAge(){
    	//计算平均年龄
    	List<Subcompartment> subcompartment = Subcompartment.find.where().eq("subcompartment", this).findList();
    	
    	double[] ages = new double[subcompartment.size()];
    	double averageAge = 0;
    	double temp = 0;
    	
    	for( int i = 0; i < subcompartment.size(); i++ ){
    		ages[i] = subcompartment.get(i).averageAge();
    	}
    	
    	for( int i = 0; i < subcompartment.size(); i++ ){
    		averageAge += ages[i] * subcompartment.get(i).area;
    		temp += subcompartment.get(i).area;
    	}
    	
    	return averageAge/temp;
    }
    
	public Float volumePerHa(){
		//单位面积蓄积量		
		return this.volume()/this.area();
	}
	public Float volume(){
		//总蓄积量
		List<Subcompartment> subcompartment = Subcompartment.find.where().eq("subcompartment", this).findList();
		Float volume = 0F;
		
		for( int i = 0; i < subcompartment.size(); i++ ){
			volume += subcompartment.get(i).volume();
		}
		
		return volume;
	}
	public Integer ageClass(){
		//龄级
		int age = (int)this.averageAge();  //可能出现bug
		
		age /= 10; //取十位数
		
		if( age % 2 == 1 ){
			return age * 10; //为奇
		}
		else{
			return (++age) * 10;
		}
		
	}
	public Float canopyDensity(){
		List<Subcompartment> subcompartment = Subcompartment.find.where().eq("subcompartment", this).findList();
		Float canopyDensity = 0F;
		for( int i = 0; i < subcompartment.size(); i++ ){
			canopyDensity += subcompartment.get(i).canopyDensity()*subcompartment.get(i).area;
		}
		return canopyDensity/this.area();
	}
}
